package com.miyuan.wm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.miyuan.wm.aop.UserOperation;
import com.miyuan.wm.mapper.CustomerMapper;
import com.miyuan.wm.pojo.CustomerEntity;
import com.miyuan.wm.request.AddCustomerRequestVo;
import com.miyuan.wm.request.QueryCustomersRequestVo;
import com.miyuan.wm.request.UpdateCustomerRequestVo;
import com.miyuan.wm.response.BaseResponse;
import com.miyuan.wm.response.PageResponse;
import com.miyuan.wm.response.QueryCustomerByIdResponseVo;
import com.miyuan.wm.response.QueryCustomersResponseVo;
import com.miyuan.wm.service.CustomerService;
import com.miyuan.wm.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName CustomerServiceImpl
 * @Description 客户管理接口业务
 */
@Service
@Slf4j
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerEntity> implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResponse<List<QueryCustomersResponseVo>> listCustomers(QueryCustomersRequestVo requestVo) {
        Page<CustomerEntity> page = new Page<>(requestVo.getPageNumber(), requestVo.getPageSize());
        Wrapper<CustomerEntity> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(requestVo.getName())) {
            wrapper.like("name", requestVo.getName());
        }
        if (StringUtils.isNotBlank(requestVo.getPersonInCharge())) {
            wrapper.like("person_in_charge", requestVo.getPersonInCharge());
        }
        if (StringUtils.isNotBlank(requestVo.getTel())) {
            wrapper.like("tel", requestVo.getTel());
        }
        wrapper.orderBy("id", false);
        List<CustomerEntity> customerEntities = customerMapper.selectPage(page, wrapper);
        List<QueryCustomersResponseVo> responseVos = BeanUtils.convert(customerEntities, QueryCustomersResponseVo.class);
        return PageResponse.newInstance(responseVos, page.getTotal(), page.getCurrent(), page.getSize());
    }

    @Override
    @UserOperation("新增客户信息")
    public BaseResponse<Boolean> addCustomer(AddCustomerRequestVo requestVo) {
        CustomerEntity entity = BeanUtils.convert(requestVo, CustomerEntity.class);
        customerMapper.insert(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("修改客户信息")
    public BaseResponse<Boolean> updateCustomer(UpdateCustomerRequestVo requestVo) {
        CustomerEntity entity = BeanUtils.convert(requestVo, CustomerEntity.class);
        entity.setId(requestVo.getId());
        customerMapper.updateById(entity);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    @UserOperation("删除客户信息")
    public BaseResponse<Boolean> deleteCustomers(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return BaseResponse.errorOf(Boolean.FALSE);
        }
        customerMapper.deleteBatchIds(ids);
        return BaseResponse.successOf(Boolean.TRUE);
    }

    @Override
    public BaseResponse<List<QueryCustomersResponseVo>> queryCustomerforSelect() {
        Wrapper<CustomerEntity> wrapper = new EntityWrapper<>();
        List<CustomerEntity> supplierEntities = customerMapper.selectList(wrapper);
        return BaseResponse.successOf(BeanUtils.convert(supplierEntities, QueryCustomersResponseVo.class));
    }

    @Override
    public BaseResponse<QueryCustomerByIdResponseVo> queryCustomerById(int id) {
        CustomerEntity customerEntity = customerMapper.selectById(id);
        return BaseResponse.successOf(BeanUtils.convert(customerEntity, QueryCustomerByIdResponseVo.class));
    }
}
